linux下打开对外开放端口号

您所在的位置:网站首页 linux 端口 开放 linux下打开对外开放端口号

linux下打开对外开放端口号

2022-03-25 00:07| 来源: 网络整理| 查看: 265

第一种方式

(1)查看对外开放的端口状态          查询已开放的端口 netstat  -ntulp | grep 端口号:可以具体查看某一个端口号          查询指定端口是否已开 firewall-cmd --query-port=666/tcp            提示 yes,表示开启;no表示未开启。

(2)查看防火墙状态          查看防火墙状态 systemctl status firewalld          开启防火墙 systemctl start firewalld            关闭防火墙 systemctl stop firewalld          开启防火墙 service firewalld start           若遇到无法开启          先用:systemctl unmask firewalld.service           然后:systemctl start firewalld.service  

(3)对外开发端口        查看想开的端口是否已开:firewall-cmd --query-port=6379/tcp        添加指定需要开放的端口:firewall-cmd --add-port=123/tcp --permanent        重载入添加的端口:firewall-cmd --reload        查询指定端口是否开启成功:firewall-cmd --query-port=123/tcp

       移除指定端口:firewall-cmd --permanent --remove-port=123/tcp  

第二种方式

             安装iptables-services : yum install iptables-services

             进入下面目录进行修改: /etc/sysconfig/iptables

          

 linux系统的端口设置在/etc/sysconfig/iptables文件中配置。使用编辑器打开该文件。内容如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

# Firewall configuration written by system-config-firewall

# Manual customization of this file is not recommended.

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT

网上说的是如下code

?

1

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3001 -j ACCEPT

我在CentOS6.5中测试上面的代码,不能成功。

如果我们需要对外开放80端口,则上面文件中添加如下code

1

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT

同时还需要注意的是,这段代码需要加入到

1

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

之后,否则端口也不能打开。最后的配如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

# Firewall configuration written by system-config-firewall

# Manual customization of this file is not recommended.

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT

编辑上面的文件 需要提供su权限.

保存上面的文件后,在终端运行如下命令:更新防火墙配置

1

service iptables restart

下面这个命令可以看到开放的端口

1

/sbin/iptables -L -n

 

下面的命令可以关闭/打开防火墙(需要重启系统)

 

1

2

开启: chkconfig iptables on

关闭: chkconfig iptables off

下面的代码可以启动和停止防火墙(立即生效,重启后失效)

 

1

2

开启: service iptables start 

关闭: service iptables stop



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3